<?php
namespace app\admin\controller;

use app\admin\logic\GoodsLogic;
use think\facade\Db;
use think\facade\View;

class GoodsCategory extends Base
{
    public function index()
    {
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('pid')){
                $map['parent_id'] = input('pid');
            }else{
                $map['parent_id'] = 0;
            }
            $count = Db::name('goods_category')->alias('c')->where($map)->count();

            $field = 'c.*';
            $list = Db::name('goods_category')
                ->alias('c')
                ->field($field)
                ->where($map)
                ->order('c.id desc ')
                ->page($page,$limit)
                ->select()->toArray();
            if($list){
                foreach($list as $k=>$v){
                    $children = Db::name('goods_category')->alias('c')->where(['parent_id'=>$v['id']])->select();
                    if($children){
                        foreach($children as $ck=>$cv){
                            $cv['children'] = Db::name('goods_category')->alias('c')->where(['parent_id'=>$cv['id']])->select();
                            $children[$ck] = $cv;
                        }
                    }
                    $list[$k]['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                    $list[$k]['children'] = $children;
                }
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }

        return View::fetch();
    }
    public function edit(){
        if(IS_POST){
            $params = input();
            $data = [];
            $data['name'] = $params['name'];
            $data['image'] = $params['image'];
            $data['parent_id'] = $params['cat1']??0;
            $data['parent_id2'] = $params['cat2']??0;
            if($data['parent_id'] && $data['parent_id2']){
                $data['level'] = 3;
            }else if($data['parent_id']){
                $data['level'] = 2;
            }else{
                $data['level'] = 1;
            }
            $data['sort'] = $params['sort'];
            $data['is_show'] = $params['is_show'];
            $data['w_time'] = getCurTime();
            if(!empty($params['id'])){
                Db::name('goods_category')->where('id',$params['id'])->update($data);
            }else{
                Db::name('goods_category')->insert($data);
            }
            return json(['status'=>1,'msg'=>'操作成功！']);
        }else{
            $pid = input('pid');
            $id = input('id');
            $info = [];
            if($id){
                $info = Db::name('goods_category')->where('id',$id)->find();
            }
            View::assign('pid',$pid);
            View::assign('info',$info);
            //商品分类
            $cats = Db::name('goods_category')->where('parent_id',0)->select()->toArray();
            foreach($cats as $k=>$v){
                $cats2 = Db::name('goods_category')->where('parent_id',$v['id'])->select()->toArray();
                $cats[$k]['child'] = $cats2;
            }
            View::assign('cats',$cats);
            return View::fetch();
        }

    }

    //获取分类
    public function getCategory(){
        $parent_id = input('parent_id');
        $clists = Db::name('goods_category')->where('parent_id',$parent_id)->select();
        return json(['status'=>1,'data'=>$clists]);
    }

    /**
     * 删除分类
     */
    public function del()
    {
        $ids = input('ids/a');
        Db::startTrans();
        Db::name('goods_category')->where(['id'=>['in',$ids]])->delete();
        Db::name('goods_category')->where(['parent_id'=>['in',$ids]])->whereOr(['parent_id2'=>['in',$ids]])->delete();
        Db::commit();
        return json(['status'=>1,'msg'=>'操作成功！']);
    }

    //选择分类
    public function choosecategory(){
        if(IS_POST){
            $id = input('post.id/d');
            $info = Db::name('goods_category')->where('id',$id)->find();
            return json(['status'=>1,'data'=>$info,'fid'=>$info['id'],'fname'=>$info['name']]);
        }else{
            return View::fetch();
        }
    }
}